package com.liury.bbs.mapper;

import com.liury.bbs.entity.Title;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Repository
public interface TitleMapper {
    @Select("select count(*) from title")
    public int counttitle();

    @Select("select count(*) from title where kind_id=#{kind_id}")
    public int counttitlebykind(Integer kind_id);



    @Select("select * from title where kind_id=#{kindid} order by uptime desc limit 1")
    @Results(id="titleMap",value = {
            @Result(property = "user",column = "user_id",one = @One(select = "com.liury.bbs.mapper.UserMapper.getById")),
            @Result(property = "user_id",column = "user_id"),
    })
    public Title newTitle(int kindid);

    @Select("select * from title where kind_id=#{kindid}")
    public List<Title> getTitlesByKid(Integer kindid);

    @Insert("insert into title(kind_id,user_id,title,content,uptime) values(#{kind_id},#{user_id},#{title},#{content},now())")
    public int insert(Title title);

    @Select("select * from title where id =#{id}")
    @ResultMap("titleMap")
    public Title getTitle(int id);

    //后台
    @Select("<script>" +
            "select * from title where 1=1\n" +
            "    <if test=\"title!=null and title!=''\">\n" +
            "         and title like concat('%',#{title},'%')\n" +
            "    </if>\n" +
            "    <if test=\"content!=null and content!=''\">\n" +
            "         and content like concat('%',#{content},'%')\n" +
            "    </if>\n" +
            "    <if test=\"kind_id!=0 and kind_id!=null\">\n" +
            "         and kind_id=#{kind_id}\n" +
            "    </if>" +
            "</script>")
    @ResultMap("titleMap")
    public List<Title> getAllTitles(Title title);

    @Delete("delete from title where id=#{id}")
    public void deleteTitle(Integer id);

    @Update("update title set title=#{title},content=#{content} where id=#{id}")
    public void updateTitle(Title t);
}
